﻿/* 
 *  <copyright file="PlatformerScreen.cs" company="Wesley Jowitt">
 *  
 *  SobrietyEngine: Copyright (c) 2011 Wesley Jowitt
 *  
 *  This software is distributed under a software license.
 *   * As of the creation time of this file, the license in use is the Microsoft Permissive License (Ms-PL) 
 *    * The license can be viewed at:
 *     * License.Txt in the root folder of this project
 *      * http://sobrietyengine.codeplex.com/license
 *       * http://www.microsoft.com/opensource/licenses.mspx
 *       
 *  </copyright>
 *   */

using System;
using Microsoft.Xna.Framework;
using SEGameExample.Kludge;
using SobrietyEngine.Actor;
using SobrietyEngine.Components;
using SobrietyEngine.Guids;
using SobrietyEngine.Logging;
using SobrietyEngine.Scene;
using SobrietyEngine.Screens;
using SobrietyEngine.UDMF;
using SEGameExample.InputMappers;
namespace SEGameExample.Screens
{
    class SceneNodeTest : BaseLevel
    {
        public SceneNodeTest()
            : base("SceneNodeTest")
        {
        }

        public override void Initialize()
        {
            base.Initialize();

            //set some logging options
            Logger.AppendLoggingType(Logger.LogType.AssertOnError |
                                     Logger.LogType.VSConsole |
                                     Logger.LogType.RaiseLogEvent);

            //set up our PlayerOne (first found user with an Xbox 360 controller)
            //turn off any controllers that may be active (shoudl always disable controllers when you are finished!)
            PlayerOne.DisableAllInputMappers();
            if (!PlayerOne.HasInputMapper(typeof(PlatformerEventMapper)))
                PlayerOne.AddInputMapper(new PlatformerEventMapper(EventRouter, PlayerOne.Guid));
            PlayerOne.EnableInputMapper(typeof(PlatformerEventMapper));

            //forward declaration of some variables used in here
            IActor actor = null;
            IScene scene = SceneGameService.Scenes[SceneName];

            //create some debug arrows, which we wil use to test the Orientation component
            IActor[] arrows = new IActor[3];

            Vector3 arrowPos = new Vector3(-2, 0, 0);
            float yaw = 0, pitch = 0, roll = 0;
            for (int i = 0; i < arrows.Length; ++i)
            {
                if (yaw > 0)
                {
                    yaw = 0;
                    pitch = 0.01f;
                }
                else if (pitch > 0)
                {
                    pitch = 0;
                    roll = 0.01f;
                }
                else
                    yaw = 0.01f;

                arrowPos.X += 3f;

                arrows[i] = SobrietyEngine.Actor.Builders.Debug.DebugArrow.Build(Scene, arrowPos);
                IComponent c = new SobrietyEngine.Components.Common.ConstantRotationCmp(yaw, pitch, roll);
                arrows[i].AttachComponent(c);

                //lets attach a scenenode acor to each of these rotating actors.
                Vector3 scale = new Vector3(0.5f, 0.5f, 0.5f);
                Vector3 attachPos = new Vector3(1,0,0);
                IActor attached = SobrietyEngine.Actor.Builders.Model.Model.Build(Scene, attachPos, Vector3.Forward, scale, "Models/UnitSphere");
                attached.AttachComponent(new SobrietyEngine.Components.SceneGraph.SceneNodeCmp(arrows[i].Guid));
            }

            actorGameService.LoadActorTemplates("actors/actorDefs");

            //some help text about controls
            String help = "SceneNode test\n ";
            help += "Left thumbstick moves\n";
            help += "(A) jumps, (RB) super jumps, \n";
            help += "(B) exits, (Y) toggles physics debug \n";
            help += "(LB) toggle debug console\n";

            SobrietyEngine.Actor.Builders.GUI.Text2D.Build(Scene, new Vector2(0, ScreenManager.FullScreenViewport.Height / 2), help);

            //create cameras
            IActor cam3d = GameplaySpawner.OrthoFollowActorCam3D(scene, ScreenManager.FullScreenViewport, ScreenManager.GraphicsDevice, arrows[1].Guid);
            IActor cam2d = SobrietyEngine.Actor.Builders.Cameras.GuiCameraBldr.Build(Scene, ScreenManager.FullScreenViewport);

            //turn on our controllers
            SceneGameService.Scenes[SceneName].EnableControllers();
        }

    }
}